package com.ishitong.platform.utils.spring;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

import com.ishitong.platform.ODApplicationConfig;
import com.ishitong.platform.ODApplicationConfigLoad;


/**
 * ODSessionFactoryBean，重载了Spring提供的LocalSessionFactoryBean，支持从Platform中取得Hibernate配置文件。
 * User: liufeng Date: 2011-10-20 Time: 11:31:36
 */
public class ODSessionFactoryBean extends LocalSessionFactoryBean
{
	/**
	 * 所有属性设置完毕后，需要进行的初始化工作
	 * 
	 * @throws Exception
	 */
	public void afterPropertiesSet() throws Exception
	{
		List<String> hibernateConfig = new ArrayList<String>();
		Map<String, ODApplicationConfig> moduleConfig = ODApplicationConfigLoad.getApplicationConfig();
		Iterator ite = moduleConfig.values().iterator();
		while (ite.hasNext())
		{
			ODApplicationConfig module = (ODApplicationConfig) ite.next();
			List<String> hbmcfg=module.getHibernateConfig();
			for (String config : hbmcfg)
			{
				hibernateConfig.add(config);
			}
		}
		String [] hibernateConfigFile=new String[hibernateConfig.size()];
		for(int i=0;i<hibernateConfig.size();i++)
		{
			hibernateConfigFile[i]=hibernateConfig.get(i);
		}
		super.setMappingResources(hibernateConfigFile);
		// 调用父类的初始化函数
		super.afterPropertiesSet();

	}

	@Override
	protected void afterSessionFactoryCreation() throws Exception
	{
		super.afterSessionFactoryCreation();
	}
}
